package com.gmail.dmitryustimov.ms.java7.chain_of_responsibility.mod.mod_multy_type_visit.testperf;

import com.gmail.dmitryustimov.ms.java7.chain_of_responsibility.mod.mod_multy_type_visit.demo.handlers1_100_request1.*;
import com.gmail.dmitryustimov.ms.java7.patternslib.chain_of_responsibility.singleevent.Handler;

public class TestPerfHandlers1_100Request1All {

    private static final int NUM_HANDLES = 100000;
    private static final int NUM_WARM_UP = 3;
    private static final int NUM_TESTS = 100;

    private static final int NUM_HANDLERS = 100;

    private static final String MESSAGE = "chain_of_responsibility_mod - visit, 1-100 handlers, 1 request types, handleChain All, took: ";

    public static void main(String[] args) {

        //warm up
        for (int i = 0; i < NUM_WARM_UP; i++) {
            test();
        }

        //tests
        long time = 0;
        for (int j = 0; j < NUM_TESTS; j++) {
            time += test();
        }
        System.out.println("final - " + MESSAGE + time/NUM_TESTS + "ns (" + (time/NUM_TESTS)/1000000 + "ms)");
    }

    private static long test() {
        Handler<Request> h1 = new CocreteHandler1<>("CocreteHandler1", 10);
        Handler<Request> beforeNext = h1;

        for (int i = 1; i < NUM_HANDLERS; i++) {
            Handler<Request> next = new CocreteHandler1<>("CocreteHandler" + (i + 1), 10);
            beforeNext.setNext(next);
            beforeNext = next;
        }

        Request request1 = new Request1("Request1", 5);

        long startTime = System.nanoTime();

        for (long j = 0; j < NUM_HANDLES; j++) {
            h1.handleChain(request1);
        }

        long estimatedTime = System.nanoTime() - startTime;
        System.out.println(MESSAGE + estimatedTime + "ns");
        return estimatedTime;
    }
}
